package net.itgoo.parkingpay.ui.edit;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import net.itgoo.parkingpay.R;

import java.util.List;

public class ParkingEditAdapter extends RecyclerView.Adapter<ParkingEditAdapter.RecyclerHolder> {
    private Context mContext;
    private List<String> mDataList;
    private ParkingEditAdapter.OnItemClickListener mOnItemClickListener;

    public ParkingEditAdapter(Context context) {
        this.mContext = context;
    }

    public void setOnItemClickListener(ParkingEditAdapter.OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setData(List<String> dataList) {
        if (null != dataList) {
            mDataList = dataList;
            notifyDataSetChanged();
        }
    }

    @Override
    public ParkingEditAdapter.RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.parking_item_edit, parent, false);
        return new ParkingEditAdapter.RecyclerHolder(view);
    }

    @Override
    public void onBindViewHolder(ParkingEditAdapter.RecyclerHolder holder, final int position) {
        holder.editPlate.setText(mDataList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }
            }
        });
        holder.editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemEdit(position);
                }
            }
        });
        holder.deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemDelete(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    class RecyclerHolder extends RecyclerView.ViewHolder {
        TextView editPlate;
        ImageButton editButton;
        ImageButton deleteButton;
        private RecyclerHolder(View itemView) {
            super(itemView);
            editPlate = itemView.findViewById(R.id.parking_item_edit_plate_tv);
            editButton = itemView.findViewById(R.id.parking_item_edit_edit_ib);
            deleteButton = itemView.findViewById(R.id.parking_item_edit_delete_ib);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
        void onItemEdit(int position);
        void onItemDelete(int position);
    }
}
